import { GATEWAY_URL } from "@/lib/env";
import Item, { ItemCategory } from "@/shared/item";
import { error } from "console";

class ItemsService {
  async getAllItems() {
    try {
      const response = await fetch(`${GATEWAY_URL}/items`, {
        method: "GET",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error getting items:", error);
      throw error;
    }
  }

  async deleteItem(id: number) {
    try {
      const response = await fetch(`${GATEWAY_URL}/items/${id}`, {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      const data = await response.json();
      if (!response.ok) throw data.message;

      return data;
    } catch (error) {
      throw error;
    }
  }

  async deleteCategory(id: number) {
    try {
      const response = await fetch(`${GATEWAY_URL}/items/categories/${id}`, {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      const data = await response.json();

      if (!response.ok) throw data.message;

      return data;
    } catch (error) {
      throw error;
    }
  }

  async createCategory(name: string) {
    try {
      const response = await fetch(
        `${GATEWAY_URL}/items/categories?name=${name}`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${localStorage.getItem("token")}`,
          },
        }
      );

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error creating category:", error);
      throw error;
    }
  }

  async createItem(item: Item & { category: ItemCategory }, file?: File) {
    try {
      const fd = new FormData();
      if (file) fd.append("file", file);
      fd.append("description", item.description);
      fd.append("name", item.name);
      fd.append("price", item.price.toString());
      fd.append("categoryId", item.categoryId.toString());
      const response = await fetch(`${GATEWAY_URL}/items`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
        body: fd,
      });

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error creating item:", error);
      throw error;
    }
  }

  async updateItemById(
    id: number,
    item: Item & { category: ItemCategory },
    file?: File
  ) {
    try {
      const fd = new FormData();
      console.log(file);
      if (file) fd.append("file", file);
      fd.append("description", item.description);
      fd.append("name", item.name);
      fd.append("price", item.price.toString());
      fd.append("categoryId", item.categoryId.toString());
      const response = await fetch(`${GATEWAY_URL}/items/${id}`, {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
        body: fd,
      });

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error updating item:", error);
      throw error;
    }
  }

  async getItemById(id: number) {
    try {
      const response = await fetch(`${GATEWAY_URL}/items/${id}`, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error fetching orders:", error);
      throw error;
    }
  }
}

export default new ItemsService();
